package com.example.spring.controller;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.spring.entity.Login;
import com.example.spring.entity.User;
import com.example.spring.service.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import net.sf.json.JSONObject;
import netscape.javascript.JSObject;
@Controller
public class controller {

	@Autowired
	private loginservice loginservice;

	@Autowired
	private studentservice studentservice;
	@GetMapping("/")
  public String index() {
	  return "index";
  }
	
	@RequestMapping("/manager")
	public String manager(Model model,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);
		
		CglibProxyFactory cglibProxyFactory =new CglibProxyFactory(studentservice);
	   studentservice proxy=(studentservice)cglibProxyFactory.creatProxy();
		//System.out.println(studentservice.searchAll().toString());
		PageInfo<User>pageInfo=new PageInfo<User>(proxy.searchAll());
		model.addAttribute("pageNum", pageInfo.getPageNum());
		model.addAttribute("pageSize", pageInfo.getPageSize());
		model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
		model.addAttribute("isLastPage",pageInfo.isIsLastPage());
		model.addAttribute("totalPages",pageInfo.getPages());
		model.addAttribute("pageInfo",pageInfo.getList());
		model.addAttribute("total",pageInfo.getTotal());
		model.addAttribute("jumpUrl","/manager?pageNum=");
		//System.out.println(pageInfo.getList());
		return "manager";
	}

	@ResponseBody
	@RequestMapping("/search")
	public String getuserandpass(HttpServletRequest request)
	{
		String user=request.getParameter("user");
		String pass=request.getParameter("pass");
		System.out.println(user+" "+pass);
 Login logins=new Login();
 logins.setUsername(user);
 logins.setPassword(pass);
		  JSONObject result = new JSONObject();
		if(this.loginservice.loginserve(logins)!=null)
			result.put("status", "success");
		else result.put("status", "fail");
		return result.toString();
	}
	@ResponseBody
	@RequestMapping("/delete")
	public String deletid(HttpServletRequest request)
	{
		String id=request.getParameter("id");
		System.out.print(id);
		JSONObject result=new JSONObject();
		if(this.studentservice.deleteUser(id)>0)
			result.put("status","success");
		else result.put("status","fail");
		return result.toString();
	}
	@ResponseBody
	@RequestMapping("/update")
	public String edit(HttpServletRequest request)
	{
		String son=request.getParameter("id");
		String name=request.getParameter("name");
		int age=Integer.valueOf(request.getParameter("age"));
		String sex=request.getParameter("sex");
		String sdept=request.getParameter("dept");
	User user=new User();
	user.setSon(son);
	user.setSname(name);
	user.setSage(age);
	user.setSsex(sex);
	user.setSdept(sdept);
	JSONObject result=new JSONObject();
	if(this.studentservice.updateUser(user)>0)
		result.put("status","success");
	else result.put("status","fail");
	return result.toString();
	}
@ResponseBody
@RequestMapping("/insert")
public String inser(HttpServletRequest request)
{
	String son=request.getParameter("id");
	String name=request.getParameter("name");
	int age=Integer.valueOf(request.getParameter("age"));
	String sex=request.getParameter("sex");
	String sdept=request.getParameter("dept");
	User user=new User();
	user.setSon(son);
	user.setSname(name);
	user.setSage(age);
	user.setSsex(sex);
	user.setSdept(sdept);
	//System.out.println(user);
   JSONObject result=new JSONObject();
   if(this.studentservice.insertUser(user)>0)
	   result.put("status","success");
   else result.put("status","fail");
   return result.toString();
}
@ResponseBody
@RequestMapping("/searchById")
public String searchById(HttpServletRequest request)
{
 String son=request.getParameter("id");
 //System.out.println("son");
 JSONObject result=new JSONObject();
 User user=studentservice.getUser(son);
 if(user!=null)
 {	 result.put("status","success");
     result.put("user", user);}
 else result.put("status", "fail");
 System.out.println(result.toString());
 return result.toString();
}
}